﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MvcSecCamStore.Models;

namespace MvcSecCamStore.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        
        SecCamStoreEntities storeDB = new SecCamStoreEntities();

        public ActionResult Index()
        {
            // Get most popular cams
            var cams = GetTopSellingCams(5);

            return View(cams);
        }

        private List<Cam> GetTopSellingCams(int count)
        {
            // Group the order details by cam and return
            // the cams with the highest count

            return storeDB.Cams
                .OrderByDescending(a => a.OrderDetails.Count())
               .Take(count)
                .ToList();
        }

    }
}
